<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<style>
    #myView {
        width: 100%;
        min-height: 200px;
    }

    #treeDiv {
        height: 100%;
        overflow-x: hidden;
    }

    #content {
        height: 100%;
        overflow-x: hidden;
        overflow-y: auto;
    }

    /* .ztree li span.button.add {
        margin-left:2px;
         margin-right: -1px;
          background-position:-144px 0;
          vertical-align:top;
      *vertical-align:middle
      } */
    .ztreeAdd {
        margin-left: 2px;
        margin-right: -1px;
        background-position: -193px 0;
        vertical-align: top;
        *vertical-align: middle
    }

</style>
<SCRIPT type="text/javascript">
    $(function () {
        var nodes = ${departList}
        var setting = {
            treeId: 'treeDemo',
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parentId",
                    rootPid: 1
                }
            },
            callback: {
//                onClick: onClick,
                beforeRemove: beforeRemove,       //删除前的回调函数
                onRemove: onRemove,			     //删除的回调函数
                beforeRename: beforeRename,       //输入名称后保存之前的回调
                onRename: onRename				 //修改后的回调
            },
            edit: {
                editNameSelectAll: true,
                enable: true,
                showRenameBtn: true,
                showRemoveBtn: true,
                removeTitle: "删除",
                renameTitle: "编辑"
            },
            view: {
                dblClickExpand: dblClickExpand, //根节点点击无效果
                showLine: true,
                addHoverDom: addDepartment,
                removeHoverDom: removeHoverDom
            }
        };


        $.fn.zTree.init($.CurrentNavtab.find("#treeDemo"), setting, nodes);
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        treeObj.expandAll(true)

        function dblClickExpand(treeId, treeNode) {
            return treeNode.level > 0;
        };

        function beforeRemove(treeId, treeNode) {
            if (treeNode.level == 0) {
                $(document).alertmsg('warn',
                    '无法删除根节点', {
                        displayMode: 'slide',
                        displayPosition: 'topcenter',
                        okName: '好的',
                        title: '提示'
                    });
                return false;
            }
            if (treeNode.level == 1) {
                if (treeNode.children != null) {
                    if (treeNode.children.length != 0) {
                        $(document).alertmsg('warn',
                            '请先删除此节点下的子部门', {
                                displayMode: 'slide',
                                displayPosition: 'topcenter',
                                okName: '好的',
                                title: '提示'
                            });
                        return false;
                    } else {
                        return true;
                    }
                }
            }
            return true;
        };
        function onRemove(event, treeId, treeNode) {
            $(this).bjuiajax('doajax', {
                url: "depart/delete.shtml",
                type: "post",
                data: "id=" + treeNode.id,
                callback: function (json) {
                    if (json.code == 0) {
                        $(document).alertmsg('ok', '删除成功', {
                            displayMode: 'slide',
                            displayPosition: 'topcenter',
                            okName: '好的',
                            title: '提示'
                        });
                    } else {
                        $(document).alertmsg('error', '删除失败', {
                            displayMode: 'slide',
                            displayPosition: 'topcenter',
                            okName: '好的',
                            title: '提示'
                        });
                    }
                }
            })
        }

        function beforeRename(treeId, treeNode, newName, isCancel) {
            if (newName.length == 0) {
                $(document).alertmsg('error', '节点名称不能为空', {
                    displayMode: 'slide',
                    displayPosition: 'topcenter',
                    okName: '好的',
                    title: '提示'
                });
                return false;
            }
            return true;
        }

        function onRename(event, treeId, treeNode, isCancel) {
            $(this).bjuiajax('doajax', {
                url: "depart/update.shtml",
                type: "post",
                data: "id=" + treeNode.id + "&name=" + treeNode.name + "&parentId=" + treeNode.parentId + "&lv=" + treeNode.lv,
                callback: function (json) {
                    if (json.code == 0) {
                        $(document).alertmsg('ok', '修改成功', {
                            displayMode: 'slide',
                            displayPosition: 'topcenter',
                            okName: '好的',
                            title: '提示'
                        });
                    } else {
                        $(document).alertmsg('error', '修改失败', {
                            displayMode: 'slide',
                            displayPosition: 'topcenter',
                            okName: '好的',
                            title: '提示'
                        });
                    }
                }
            })
        }

        function addDepartment(treeId, treeNode) {
            console.log(JSON.stringify(treeNode))
            var sObj = $("#" + treeNode.tId + "_span");
            if ($("#" + treeNode.tId + "_add").length > 0) return;
            var addStr = "<span class='button add ztreeAdd' id='" + treeNode.tId
                + "_add' title='添加子节点' treenode_add style onfocus='this.blur();'></span>";
            if (treeNode.level != 2) {
                sObj.after(addStr);
            }
            var btn = $("#" + treeNode.tId + "_add");
            if (btn) btn.bind("click", function () {
                $(this).bjuiajax('doajax', {
                    url: 'depart/add.shtml?parentlv=' + treeNode.lv + '&pid=' + treeNode.id,
                    callback: function (json) {
                        if (json.code == 0) {
                            $(this).navtab('refresh');
                        } else {
                            alert(json.msg)
                        }
                    }
                });
                return false;
            });
        };
        function removeHoverDom(treeId, treeNode) {
            $("#" + treeNode.tId + "_add").unbind().remove();
        };

        function onClick(event, treeId, treeNode, clickFlag) {
            $(this).bjuiajax('doLoad', {
                target: $.CurrentNavtab.find("#myView"),
                url: "depart/info.shtml?id=" + treeNode.id
            });
        };

    });
</SCRIPT>
<div class="bjui-pageContent">
    <span id="total" name="total" hidden>${requestScope.total }</span>
    <div class="row" style="margin:0px;height:100%;">
        <div class="col-md-3" style="height:100%;">
            <div id="treeDiv">
                <ul id="treeDemo" class="ztree"></ul>
            </div>
        </div>
        <div class="col-md-9" style="height:60%;">
            <div id="content">
                <div id="myView" name="myView" class="col-md-12">
                    <!-- 内嵌页面1 -->
                </div>
            </div>
        </div>
    </div>
</div>